package com.teen9g.infoplatformserver.controller;

import com.github.pagehelper.PageInfo;
import com.teen9g.dto.TeachingBuildingDTO;
import com.teen9g.entity.TeachingBuilding;
import com.teen9g.infoplatformserver.service.TeachingBuildingService;
import com.teen9g.result.Result;
import com.teen9g.userVO.TeachingBuildingUserVO;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jettison.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 教学楼信息控制类
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/teachingBuilding")
public class TeachingBuildingController {

    @Autowired
    private TeachingBuildingService teachingBuildingService;

    /**
     * 添加教学楼信息
     *
     * @param teachingBuildingDTO 教学楼信息DTO
     * @return 添加结果
     */
    @PostMapping("/add")
    public Result<Integer> add(@RequestBody TeachingBuildingDTO teachingBuildingDTO) {
        log.info("添加教学楼信息:{}", teachingBuildingDTO);
        int result = teachingBuildingService.add(teachingBuildingDTO);
        return Result.success(result);
    }

    /**
     * 根据id更新教学楼信息
     *
     * @param teachingBuildingDTO 前端传来的修改后的教学楼信息
     * @param id                  修改的教学楼信息id
     * @return 修改结果 1修改成功 -1修改失败 999无变化
     */
    @PutMapping("/update/{id}")
    public Result<Integer> update(@RequestBody TeachingBuildingDTO teachingBuildingDTO
            , @PathVariable Integer id) throws JSONException {
        log.info("更新教学楼信息:{}", teachingBuildingDTO);
        int result = teachingBuildingService.update(teachingBuildingDTO, id);
        return Result.success(result);
    }

    /**
     * 根据id删除教学楼信息
     *
     * @param id 教学楼信息id
     * @return 删除结果 1删除成功 -1删除失败
     */
    @DeleteMapping("/delete/{id}")
    public Result<Integer> delete(@PathVariable Integer id) {
        log.info("删除id为{}的教学楼信息", id);
        int result = teachingBuildingService.delete(id);
        return Result.success(result);
    }

    /**
     * 根据id获取教学楼信息
     *
     * @param id 教学楼信息id
     * @return 教学楼信息
     */
    @GetMapping("/get/{id}")
    public Result<TeachingBuilding> get(@PathVariable Integer id) {
        log.info("获取id为{}的教学楼信息", id);
        TeachingBuilding teachingBuilding = teachingBuildingService.getById(id);
        return Result.success(teachingBuilding);
    }

    @PostMapping("/addView/{id}")
    public Result<String> addView(@PathVariable Integer id) {
        log.info("增加id为{}的教学楼信息浏览量", id);
        teachingBuildingService.addView(id);
        return Result.success();
    }

    /**
     * 分页获取所有教学楼信息
     *
     * @param pageNum  当前页码
     * @param pageSize 每页显示数量
     * @return 分页查询结果
     */
    @GetMapping("/page")
    public Result<PageInfo<TeachingBuilding>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer buildingId,
            @RequestParam(required = false) String roomNumber
    ) {
        log.info("分页获取所有教学楼信息,当前页码:{},每页显示数量:{}," +
                "请求参数：{}，{}", pageNum, pageSize, buildingId, roomNumber);
        PageInfo<TeachingBuilding> pageInfo =
                teachingBuildingService.getpage(pageNum, pageSize, buildingId, roomNumber);
        return Result.success(pageInfo);
    }

    /**
     * 用户个人信息分页获取所有教学楼信息
     *
     * @param pageNum  当前页码
     * @param pageSize 每页显示数量
     * @return 分页查询结果
     */
    @GetMapping("/page/user")
    public Result<PageInfo<TeachingBuildingUserVO>> pageUser(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = true) String contentAuthor
    ) {
        log.info("用户个人信息分页获取所有教学楼信息,当前页码:{},每页显示数量:{}", pageNum, pageSize);
        PageInfo<TeachingBuildingUserVO> pageInfo =
                teachingBuildingService.getpageUser(pageNum, pageSize, contentAuthor);
        return Result.success(pageInfo);
    }
}
